<template>
    <view @longtap.stop="longtap">
		<canvas :canvas-id="item.id" :id="item.id" :style="{width:size,height: size}" v-for="(item,index) in listCode" :key="item.id"></canvas>
	</view>
</template>
<script>
	import { QRCode,GetCodeImg} from '../../js_sdk';
	import { getUUid, deepClone } from '../../common/helper.js'
	export default {
		name: 'WQrcode',
		props:{
			options:{
				type: Object,
				required: true,
				default: () =>{
					return {
						
					}
				}
			}
		},
		data () {
			return {
				listCode:[],
				id: getUUid(),
				size: null,
			}
		},
		created() {
			this.size = this.options.size + 'rpx';
			this.SpecialTreatment(this.options)
			this.$nextTick(()=>{
				this.generateCode();
			})
		},
		watch: {
			options:{
				deep: true,
				handler (val) {
					const res = uni.getSystemInfoSync();
					this.size = val.size +'rpx';
					this.SpecialTreatment(val)
					setTimeout(()=>{// h5平台动态改变canvas大小
						this.generateCode();
					},50)
				}
			}
		},
		methods: {
			longtap (e){
				this.$emit('longtap',e)
			},
			SpecialTreatment (val) {//微信小程序渲染多个canvas特殊处理
					let obj = deepClone(val);
					obj.id = this.id;
					this.listCode = [obj]
			},
			generateCode () {
				try{
					const parameter = {...this.options,id: this.id,ctx: this};
					QRCode(parameter,this.calMethod)
				}catch(err){}
			},
			calMethod (res){
				this.$emit('generate',res)
			},
			async GetCodeImg (){
				try{
					const res = await GetCodeImg({id: this.id,width: this.options.size,height: this.options.size,ctx: this});
					return res
				}catch(e){}
			}
		}
	}
</script>